<?php
require_once 'database/profile_handler.php';
require_once 'database/photo_handler.php';
require_once 'database/wall_handler.php';
require_once 'database/follower_handler.php';

$u = $_SESSION['Username'];
session_start();
?>
<?php
$pf = new profile_handler();

$pf->get_nama_lengkap($_SESSION['Username']);
$nama = $pf->result[0];

$u = $_SESSION['Username'];

$ph = new photo_handler();

//ambil profpic id
$pf->retr_details($u, 4);
$id = $pf->result[0];


//ambil url
$ph->get_url($id);
?>
<div id="wall_post">
<?php
            $wall = new wall_handler();
            $wall->retr_post_byUsr($u);
            $res = $wall->result;

            if (count($wall->result) != 0) {
                //dibatasin tiga aja deh..
                // echo comment dibatesin tiga pake ajax..
                        //$c = 0;
                        if (count($wall->result) <= 3) {
                            $c = count($wall->result);
                           // echo count($wall->result);
                        }
                        else {
                            $c = 3;
                        }
                for ($i = 0; $i < $c; $i++) {
                    $parsed[$i] = $wall->parse_wall($res[$i]);
                    $tag = $wall->parse_walltagged($parsed[$i]);
                    $commentator = $wall->parse_wallcommentator($parsed[$i]);
                    $comment_body = $wall->parse_wallcommbody($parsed[$i]);

                    //for retrieving user posted
                    $userposted = $wall->result_usrposted[$i];
                    $profile_usrposted = new profile_handler();
                    $profile_usrposted->retr_details($userposted, 5);
                    $photo_usrposted = new photo_handler();
                    $photo_usrposted->get_url($profile_usrposted->result[3][0]);
?>
                    <div class="wall_container">
                        <img id="profpic_small" src="<? echo $photo_usrposted->result[0]; ?>" alt="" />
                        <div id="wall_name"><a id="link_name" href="otherprofile.php?UserTujuan=<?php echo $userposted; ?>"><?php echo $profile_usrposted->result[4][0]; ?></a></div>
                        <div id="wall_delete"><a href="home.php?delete_wall=<?php echo $wall->result_wallid[$i]; ?>">x</a></div>
                        <div id="wall_body">
<?php
                    $cekdulu = strstr($parsed[$i][0], "@\"");
                    if (!$cekdulu) {
                        // echo wall post
                        echo $parsed[$i][0];
                    } else {
                        // disini parsing tagging
                        // Masih Nge BUG, cek lagi ya??? :D
                        // Kalo tag nya salah jadi salah semua..
                        $masuk_tag = false;
                        $end_tag = false;
                        $tambah_akhir = true;
                        for ($idx = 0; $idx < strlen($parsed[$i][0]) - 1; $idx++) {
                            if ($parsed[$i][0][$idx] == '@' && $parsed[$i][0][$idx + 1] == '"' && !$masuk_tag) {
                                $nama = "";
                                $masuk_tag = true;
                                $idx+=2;
                                if ($idx >= strlen($parsed[$i][0])) {
                                    $masuk_tag = false;
                                    $idx-=2;
                                }
                            } else if ($parsed[$i][0][$idx] == '"' && $masuk_tag) {
                                $masuk_tag = false;
                                $end_tag = true;
                            } else if (!$masuk_tag) {
                                $hasil_parsing .= $parsed[$i][0][$idx];
                            }

                            if ($masuk_tag) {
                                $nama .= $parsed[$i][0][$idx];
                                //echo $parsed[$i][0][$idx];
                            }
                            if ($idx == (strlen($parsed[$i][0]) - 2) && $masuk_tag && $parsed[$i][0][$idx + 1] == '"') {
                                $masuk_tag = false;
                                $end_tag = true;
                                $tambah_akhir = false;
                            } else if ($idx == (strlen($parsed[$i][0]) - 2) && $masuk_tag && $parsed[$i][0][$idx + 1] != '"') {
                                $end_tag = false;
                                $tambah_akhir = false;
                                $hasil_parsing = $parsed[$i][0];
                            }

                            if ($end_tag) {
                                $query = "SELECT usr_email FROM user WHERE nama_lengkap='$nama'";
                                $prof_handler2 = new profile_handler();
                                $prof_handler2->exec_q($query, 'usr_email');
                                if ($prof_handler2->result[0] != NULL) {
                                    $hasil_parsing .= "<a id='link_name' href='otherprofile.php?UserTujuan=";
                                    $hasil_parsing .= $prof_handler2->result[0];
                                    $hasil_parsing .= "'>";
                                    $hasil_parsing .= $nama;
                                    $hasil_parsing .= "</a>";
                                } else {
                                    $tambah_akhir = false;
                                    $hasil_parsing = $parsed[$i][0];
                                    $idx = strlen($parsed[$i][0]) - 1;
                                }
                                $end_tag = false;
                            }
                        }
                        if ($tambah_akhir)
                            $hasil_parsing .= $parsed[$i][0][$idx];
                        echo $hasil_parsing;
?>
                     <?php
                            $hasil_parsing = "";
                        }
                        if (count($commentator) > 3){
?>
                         <div id="view_more_com<?php echo "_". $i;?>" align="center">
                            <!-- tambahin commentator[$i], tambahin comment_body jg?? pake POST aja apa? -->
                            <?php
                                //parsing dulu
                                $comm_str = "";
                                for ($j=0; $j<count($commentator); $j++){
                                    if ($commentator[$j] != null){
                                        $comm_str = $comm_str. $commentator[$j]. ";";
                                    }
                                }
                                //edan panjang juga ya
                                $comm_body = "";
                                for ($j=0; $j<count($comment_body); $j++){
                                    if ($comment_body[$j] != null){
                                        $comm_body = $comm_body. $comment_body[$j]. ";";
                                    }
                                }

                                //for ($j=0; $j<count($commentator); $j++){
                                //    echo $comm_str;
                                //}

                            ?>
                            <a href="javascript:viewMoreWallComment(<?php echo $wall->result_wallid[$i]; ?>,<?php echo count($commentator)-1; ?>,<?php echo $i;?>, '<?php echo $comm_str; ?>','<?php echo $comm_body; ?>','none')" id="headerLink">view more comments...</a>
                        </div>


<?php
                        }
                        ?>

                        </div>
                        <?php

                        for ($j = 0; $j < 3; $j++) {
                            // echo commentator
                            if (strlen($comment_body[$j]) > 0) {
                                $commentator_prof = new profile_handler();
                                $commentator_prof->retr_details($commentator[$j], 5);
                                $photo_handler = new photo_handler();
                                $photo_handler->get_url($commentator_prof->result[3][0]);
?>
                                <div id="wall_comment">
                                    <img id="image_commentator" src="<?php echo $photo_handler->result[0]; ?>" alt="" />
                                    <div id="name_commentator"><a id="link_name" href="otherprofile.php?UserTujuan=<?php echo $commentator[$j]; ?>">
<?php
                                echo $commentator_prof->result[4][0];
?>
                                </a>
                            </div>
                            <div id="comment_delete"><a href="home.php?comment_wallid=<?php echo $wall->result_wallid[$i]; ?>&commentator=<?php echo $commentator[$j]; ?>&comment=<?php echo $comment_body[$j]; ?>">x</a></div>
                            <div id="comment_content">

<?php
                                    // echo comment body
                                    echo $comment_body[$j];
?>
                                </div>
                            </div>
<?php
                                }
                            }

?>
                        <div id="more_comments<?php echo "_". $i;?>"></div>

                            <form method="post" action="home.php<?php echo '?wallid=' . $wall->result_wallid[$i]; ?>">
                                <div id="insert_comment">
                                    <textarea class="textarea" name="comment_text" size="50px" rows="2" onFocus="if (this.value=='Add a comment') { this.value = '';}" onBlur="if (this.value=='') { this.value = 'Add a comment';}">Add a comment</textarea>
                                    <input type="submit" name="SubmitComment" id="SubmitComment" value="Submit"/>
                                </div>
                            </form>

                        </div>

<?php
                        }
                        if (count($wall->result) > 3){
                            ?>
                            <div id="more_posts">
                            </div>
                            <div id="view_more" align="center">
                                <a href="javascript:viewMoreWallPost(<?php echo count($wall->result); ?>,'none')" id="headerLink">view more posts...</a>
                            </div>

                            <?php
                        }
                    } else {
                        echo "Wall is empty";
                    }
?>
                </div>